﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Commands;
using PloobsEngine.Input;

namespace PloobsEngine.DataAquisition
{
    /// <summary>
    /// TODO
    /// </summary>
    public class GenericNumberColumFileSaver : DataHandlers
    {       

       public GenericNumberColumFileSaver(String fileName,string[] columns , Keys saveKey)
        {
            this.fileName = fileName;
            this.saveKey = saveKey;
            this.columns = new List<string>();
            this.columns.Add("TotalTimeElapsed");
            this.columns.Add("LastFrameTimeElapsed");
            this.columns.Add("FrameCount");
            this.columns.AddRange(columns);

            InputPlayableKeyBoard ip1 = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, saveKey, saveFile, EntityType.TOOLS);
            BindKeyCommand mm = new BindKeyCommand(ip1, BindAction.ADD);
            CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm, InputAdvanced.MyName);          
        }

        List<string> columns;

        private void raw()
        {
            StreamWriter sw = new StreamWriter(fileName + ".csv", false);
            for (int i = 0; i < columns.Count; i++)
            {
                if (i == columns.Count - 1)
                {
                    sw.Write(columns[i]+ "\n");
                }
                else
                {
                    sw.Write(columns[i] + " , ");
                }
            }


            for (int i = 0; i < values.Count; i++)
            {
                for (int j = 0; j < values[i].Numbers.Count(); j++)
                {
                    if (j == values[i].Numbers.Count() - 1)
                    {
                        sw.Write(values[i].Numbers[j] + "\n");
                    }
                    else
                    {
                        sw.Write(values[i].Numbers[j] + " , ");
                    }
                }
            }
            
            sw.Close();
            values.Clear();
            
        }
     

        private void saveFile(InputPlayableKeyBoard ipk)
        {    
             raw();
            
        }
        
        #region DataHandlers Members
        private String fileName = null;       
        Keys? saveKey = null;
        private List<GenericNumberColumDataFormat> values = new List<GenericNumberColumDataFormat>();
       
                
        public void HandleData(DataFormat data)
        {
            if (data is GenericNumberColumDataFormat)
            {
                GenericNumberColumDataFormat dat = data as GenericNumberColumDataFormat;                
                values.Add(dat);                
            }
        }

        public Type[] GetHandledType
        {
            get { return new Type[] { typeof(GenericNumberColumDataFormat) }; }
        }

        #endregion
    }
}
